<?php
	class nguoidungdb
	{
		public function Them($MaNguoiDung = "",
							 $MatKhau = "",
							 $Ho = "", 
							 $Ten = "",
						     $DiaChi = "",
							 $DienThoai = "",
							 $GioiTinh = 0,
							 $NgaySinh = "",
							 $Email = "",
							 $NgayThamGia = "",
				 			 $MaQuyen = 0,
							 $MaPhongBan = "",
							 $MaChucVu = 0, 
							 $MaPhongGD = "", 							 
							 $Khoa = 0)
		{
			$sql = "INSERT INTO nguoidung(MaNguoiDung, MatKhau, Ho, Ten, DiaChi, DienThoai, GioiTinh, NgaySinh, Email, NgayThamGia, MaQuyen, MaPhongBan, MaChucVu, MaPhongGD, Khoa)
					VALUES('$MaNguoiDung',
							'$MatKhau', 
							'$Ho', 
							'$Ten',
							'$DiaChi',  
							'$DienThoai', 
							$GioiTinh, 
							'$NgaySinh', 
							'$Email',
							'$NgayThamGia', 
							$MaQuyen,
							'$MaPhongBan',
							$MaChucVu,
							'$MaPhongGD',
							$Khoa)";				
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaNguoiDung = "",
								$MatKhau = "",							
						     	$Ho = "",
								$Ten = "",
						     	$DiaChi = "",
						     	$DienThoai = "",
						     	$GioiTinh = 0,
						     	$NgaySinh = "", 
							 	$Email = "",
							 	$MaQuyen = 0,	
							 	$MaPhongBan = "",
							 	$MaChucVu = 0,
							 	$MaPhongGD = "")
		{
			$sql = "UPDATE nguoidung SET 
			Ho = '$Ho',
			Ten = '$Ten',
			DiaChi = '$DiaChi',
			DienThoai = '$DienThoai',
			GioiTinh = $GioiTinh,
			NgaySinh = '$NgaySinh',
			Email = '$Email'";
			
			if($MaQuyen > 0)
				$sql .= ", MaQuyen = $MaQuyen";
			
			if($MaPhongBan != "")
				$sql .= ", MaPhongBan = '$MaPhongBan'";
			
			if($MaChucVu > 0)
				$sql .= ", MaPChucVu = $MaChucVu";
			
			if($MaPhongGD != "")
				$sql .= ", MaPhongGD = '$MaPhongGD'";
			
			if($MatKhau != "")
				$sql .= ", MatKhau = '$MatKhau'";
			
			$sql .= " WHERE MaNguoiDung = '$MaNguoiDung'";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}

		public function LayDanhSach($MaNguoiDung = "",
									$MatKhau = "", 
									$Ho = "",
							 	 	$Ten = "",
									$DienThoai = "",
							 	 	$GioiTinh = 0,
									$NgaySinh = "",
									$Email = "",
							 		$NgayThamGia = "",
							 		$MaQuyen = 0,	
							 		$MaPhongBan = "",
							 		$MaChucVu = 0,
							 		$MaPhongGD = "",
									$Khoa = 0,
									$TimPhongBan= "")
		{
			$sql = "SELECT * 
					FROM nguoidung
					WHERE (1)";
			
			if($MaNguoiDung != "")
				$sql .= " AND MaNguoiDung = '$MaNguoiDung'";

			if($MatKhau != "")
				$sql .= " AND MatKhau = '$MatKhau'";
			
			if($Ho != "")
				$sql .= " AND Ho = '$Ho'";
			
			if($Ten != "")
				$sql .= " AND Ten = '$Ten'";
			
			if($DienThoai != "")
				$sql .= " AND DienThoai = '$DienThoai'";
			
			if($GioiTinh > 0)
				$sql .= " AND GioiTinh = $GioiTinh";
			
			if($NgaySinh != "")
				$sql .= " AND NgaySinh = '$NgaySinh'";
			
			if($Email != "")
				$sql .= " AND Email = '$Email'";
			
			if($NgayThamGia != "")
				$sql .= " AND NgayThamGia = '$NgayThamGia'";
			
			if($MaQuyen > 0)
				$sql .= " AND MaQuyen = $MaQuyen";
			
			if($MaPhongBan != "")
				$sql .= " AND MaNguoiDung like '%$MaPhongBan%'";
			
			if($MaChucVu > 0)
				$sql .= " AND MaChucVu = $MaChucVu";
			
			if($MaPhongGD != "")
				$sql .= " AND MaPhongGD = '$MaPhongGD'";
			
			if($Khoa > 0)
				$sql .= " AND Khoa = $Khoa";
			
			if($TimPhongBan != "")
				$sql .= " AND MaNguoiDung like '%$TimPhongBan%'";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function CapNhatMatKhau($MaNguoiDung = "", $MatKhauMoi="")
		{
			$sql = "update nguoidung set MatKhau = '".$MatKhauMoi."'
					where MaNguoiDung = ".dataservice::QuoteSmart($MaNguoiDung);
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CapNhatKhoa($MaNguoiDung = "",
									$Khoa = 0)
		{
			$sql = "update nguoidung set Khoa = ".dataservice::QuoteSmart($Khoa)."
					where MaNguoiDung = ".dataservice::QuoteSmart($MaNguoiDung);
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function TonTaiTrongKeKhaiGiaoDich($MaNguoiDung = "")
		{
			$sql = "SELECT * FROM kekhaigiaodich WHERE 1";
			if($MaNguoiDung != "")
				$sql.= " AND MaNguoiDung = '$MaNguoiDung'";
		
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function Xoa($MaNguoiDung = "")
		{
			$sql = "DELETE 
					FROM nguoidung
					WHERE MaNguoiDung = '$MaNguoiDung'";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function XoaTatCa()
		{
			$sql = "DELETE FROM nguoidung where MaQuyen <> 1";			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
	}
?>